package com.itheima.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class MyBatisConfig {
//      <!--spring整合mybatis后控制的创建连接用的对象-->
//    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
//        <property name="dataSource" ref="dataSource"/>
//        <property name="typeAliasesPackage" value="com.itheima.domain"/>
//    </bean>



    @Bean//这里之所以可以用@@Autowired DataSource dataSource，是因为JDBCConfig文件的@Bean里指明了Bean的名字
    public SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource dataSource){
          //空参构造
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
//        起别名
        ssfb.setTypeAliasesPackage("com.itheima.domain");
        ssfb.setDataSource(dataSource);

        return ssfb;
    }
//      <!--加载mybatis映射配置的扫描，将其作为spring的bean进行管理-->
//    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
//        <property name="basePackage" value="com.itheima.dao"/>
//    </bean>

    @Bean
    public MapperScannerConfigurer getMapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.itheima.dao");
        return msc;
    }
}
